package com.tjc.download.client;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Author: tjc
 * @description
 * @Date Created in 18:02 2022-09-18
 */
public class Main {
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://127.0.0.1:8080/download/2/02-Functions.pdf");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(false);
        conn.setRequestMethod("GET");
        conn.setUseCaches(true);
        conn.setRequestProperty("Range", "bytes=0-1023, 1024-");
        conn.connect();
        int code = conn.getResponseCode();
        System.out.println("code:" + code);
        InputStreamReader reader = new InputStreamReader(conn.getInputStream(), "UTF-8");
        File file = new File("output.pdf");
        if (!file.exists())
            file.createNewFile();
        FileOutputStream log = new FileOutputStream(file);
        byte[] buff = new byte[102 * 1024 * 10];
        int i = 0;
        while ((i = conn.getInputStream().read(buff)) > 0) {
            log.write(buff, 0, i);
        }
        log.close();
    }
}
